package com.xing.photo.funcation;

import com.xing.photo.bean.PhotoUser;
import com.xing.photo.dao.UserDao;
import com.xing.photo.util.StaticUtil;
import com.xing.photo.util.VarConfig;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Created with IntelliJ IDEA.
 * User: xingzhe
 * Date: 2015/3/11
 * Time: 18:44
 */
public class ImgUrl {
    private static final ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
    VarConfig config = (VarConfig) ac.getBean("config");
    UserDao userDao = (UserDao) ac.getBean("userDao");

    public static void main(String[] args) throws URISyntaxException, IOException {
        ImgUrl imgUrl = new ImgUrl();
        imgUrl.printImg(30);
    }

    private void printImg(Integer uid) throws IOException {
        PhotoUser user = userDao.getUser(uid);
        System.out.println(user.getName() + " [" + 40 + "P]");
        for (int i = 1; i < 4; i++) {
            URI uri = getUri(user, i);
            String imgContent = getContent(uri);
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode imgNode = objectMapper.readTree(imgContent);
            JsonNode dataNode = imgNode.get("data").get("photo_list");
            for (int j = 0; j < dataNode.size(); j++) {
                System.out.println("<img src=\"http://ww3.sinaimg.cn/large/" + dataNode.get(j).get("pic_name").asText() + "\"/>");
            }
        }

    }

    private URI getUri(PhotoUser user, int pageBegin) {
        URI uri = null;
        try {
            uri = new URIBuilder().setScheme(config.getUriScheme()).setHost(config.getHost()).setPath(config.getPath())
                    .setParameter("uid", user.getUid() + "")
                    .setParameter("album_id", user.getAlbumId() + "")
                    .setParameter("count", config.getCount() + "")
                    .setParameter("page", pageBegin + "")
                    .setParameter("type", config.getType() + "").build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return uri;
    }

    private String getContent(URI uri) {
        String result = null;
        try {
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("Cookie", StaticUtil.cookies);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            CloseableHttpResponse response = httpClient.execute(httpGet);
            HttpEntity re = response.getEntity();
            result = EntityUtils.toString(re, config.getCharset());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
